package ua.com.stormlabs.tiny.licenseserver;

import org.hibernate.cfg.Configuration;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceUnitInfo;
import java.util.Map;

/**
 * @author Denis Bondarenko
 * @version 1.0 4/28/11
 */
public class HibernateSchemaExporter {

    private static final String HIBERNATE_DIALECT = "org.hibernate.dialect.MySQLInnoDBDialect";
    private static final String HIBERNATE_SCHEMA = "tiny_lcs_server";
//    private static final String HIBERNATE_DIALECT = "org.hibernate.dialect.HSQLDialect";

    public static void main(String[] args) throws Exception {
        FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("src/test/resources/test-app-config.xml");
        LocalContainerEntityManagerFactoryBean entityManagerFactory =
                (LocalContainerEntityManagerFactoryBean) context.getBean("&entityManagerFactory");

        PersistenceUnitInfo persistenceUnitInfo = entityManagerFactory.getPersistenceUnitInfo();
        Map jpaPropertyMap = entityManagerFactory.getJpaPropertyMap();
        Configuration configuration = new Ejb3Configuration().configure(persistenceUnitInfo, jpaPropertyMap).getHibernateConfiguration();

        SchemaExport schemaExport = new SchemaExport(configuration);
        schemaExport.setOutputFile("schema.sql");
        schemaExport.setFormat(true);

        schemaExport.create(true, false);
//        schemaExport.execute(false, false, false, true);
    }

}
